//
//  AppDelegate.swift
//  我的微博
//
//  Created by shmily on 15/7/28.
//  Copyright © 2015年 Stanford-IOS8-Swift-shmilyAshen. All rights reserved.
//

import UIKit

///通知
let switchVCNotification = "switchVCNotification"

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?



    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        ///设置全局属性
        setPara()
        window = UIWindow(frame: UIScreen.mainScreen().bounds)
        //确定登陆界面
        window?.rootViewController = showViewController()
        window?.makeKeyAndVisible()
        
        //通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "switchController:", name: switchVCNotification, object: nil)

        return true
    }
    
    ///习惯写法,实际上没有作用
    deinit{
        NSNotificationCenter.defaultCenter().removeObserver(self)
    }
    
    // MARK:- 通知切换控制器
    //单击"立即体验"按钮发送通知 ->主控制器
    //欢迎界面动画结束发送通知 ->主控制器
    //授权成功后发送通知 ->欢迎回来
     func switchController(info: NSNotification) {
        let isMainVC = info.object as! Bool
        
        window?.rootViewController = isMainVC ? MainBC() : WelcomeVC()
    }
    
    
    
    //MARK:- 登陆界面的选择
    ///确定登陆界面
    private func showViewController() -> UIViewController {
        //登陆成功 且 新版本 -> 新特性界面 否则 欢迎界面
        //登陆失败 主界面
        if UserInfo.isLogon {
            return isNewVersion() ? NewFeatureCVC() : WelcomeVC()
        }
        
        return MainBC()
    }
    ///判断是否为新版本
    private func isNewVersion() ->Bool {
        let nowVersion = Double(NSBundle.mainBundle().infoDictionary!["CFBundleShortVersionString"] as! String)!
        
        let savedVersion = NSUserDefaults.standardUserDefaults().doubleForKey("currentVersion")
        //写入文件
        NSUserDefaults.standardUserDefaults().setDouble(nowVersion, forKey: "currentVersion")
        return nowVersion > savedVersion
    }
    
    ///设置全局属性
    func setPara() {
        UITabBar.appearance().tintColor = UIColor.orangeColor()
        UINavigationBar.appearance().tintColor = UIColor.orangeColor()
    
    }
   
}

